//
// Created by ZLY on 2025/5/11.
//

#include "hud_text.h"

HUDText *HUDText::addHUDTextChild(Object *parent, const std::string &text, glm::vec2 size, glm::vec2 render_pos,
                                  const std::string &font_path,
                                  int font_size,
                                  const std::string &bg_path, Anchor anchor) {
    auto hudText = new HUDText();
    hudText->init();
    hudText->setRenderPos(render_pos);
    hudText->setSprite(Sprite::addSpriteChild(hudText, bg_path, 1, anchor));
    hudText->setSize(size);
    hudText->setTextLabel(TextLabel::addTextLabelChild(hudText, text, font_path, font_size, anchor));
    if (parent) {
        parent->addChild(hudText);
    }
    return hudText;
}

void HUDText::setBgSizeByText(float margin) {
    auto text_size = text_label_->getSize();
    setSize(text_size + glm::vec2(margin, margin));
}

void HUDText::setSize(glm::vec2 size) {
    size_ = size;
    sprite_bg_->setSize(size_);
}

void HUDText::setBackGround(const std::string &file_path) {
    if (sprite_bg_) {
        sprite_bg_->setTexture(Texture(file_path));
    } else {
        sprite_bg_ = Sprite::addSpriteChild(this, file_path, 1, Anchor::CENTER);
    }
}
